package com.acmedcare.framework.exchange.center.endpoint.cqrs;

import com.acmedcare.framework.exchange.center.entity.bean.Task;
import com.acmedcare.framework.exchange.center.entity.dto.TaskCommandEvent;
import com.acmedcare.framework.exchange.center.repository.TaskRepository;
import com.acmedcare.framework.starter.bus.AcmedcareEventListener;
import com.acmedcare.framework.starter.bus.AcmedcareEventPublisher;
import org.springframework.stereotype.Component;

@Component
public class TaskCommandListener extends AcmedcareEventListener<TaskCommandEvent> {

  private final TaskRepository repository;

  public TaskCommandListener(AcmedcareEventPublisher publisher,
      TaskRepository repository) {
    super(publisher);
    this.repository = repository;
  }

  @Override
  public void onAcmedcareEvent(TaskCommandEvent taskCommandEvent) {

    switch (taskCommandEvent.getActionCommand()) {
      case Update:
        Task task = repository.selectById(taskCommandEvent.getData().getTaskId());
        if (task == null) {
          return;
        }

        task.setExecuteTime(taskCommandEvent.getData().getExecuteTime());
        task.setLastRunTime(taskCommandEvent.getData().getLastRunTime());
        task.setStatus(taskCommandEvent.getData().getStatus());
        repository.updateById(task);
    }
  }
}
